<?php

namespace app\admin\controller\subject;

// 引入基类控制器
use app\common\controller\Backend;

class Info extends Backend
{
    // 无需鉴权的方法,但需要登录
    protected $noNeedRight = ['index'];

    // 设置关联查询
    protected $relationSearch = true;

    protected $SubjectModel;
    protected $OrderModel;
    protected $CommentModel;

    public function __construct()
    {
        parent::__construct();

        $this->model = model('Subject.Subject');
        $this->SubjectModel = model('Subject.Subject');
        $this->OrderModel = model('Subject.Order');
        $this->CommentModel = model('Subject.Comment');
    }

    public function index()
    {
        return $this->view->fetch();
    }

    public function order($ids = NULL)
    {
        $subject = $this->SubjectModel->find($ids);

        if(!$subject){
            $this->error(__('No Results were found'));
            exit;
        }

        // 切换模型
        $this->model = $this->OrderModel;

        // 过滤请求参数中的html标签和取消两边你空白
        $this->request->filter(['strip_tags', 'trim']);
        // 获取查询参数
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $total = $this->model
            ->with(['business'])
            ->where(['subid' => $ids])
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->count();

        $list = $this->model
            ->with(['business'])
            ->where(['subid' => $ids])
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

        $result = [
            'rows' => $list,
            'total' => $total
        ];

        return json($result);
    }

    public function comment($ids = NULL)
    {
        $subject = $this->SubjectModel->find($ids);

        if(!$subject){
            $this->error(__('No Results were found'));
            exit;
        }

        // 切换模型
        $this->model = $this->CommentModel;

        // 过滤请求参数中的html标签和取消两边你空白
        $this->request->filter(['strip_tags', 'trim']);
        // 获取查询参数
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $total = $this->model
            ->with(['subject', 'business'])
            ->where(['subid' => $ids])
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->count();

        $list = $this->model
            ->with(['subject', 'business'])
            ->where(['subid' => $ids])
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

        $result = [
            'rows' => $list,
            'total' => $total
        ];

        return json($result);
    }
}
